package com.shouxun.adapter;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import com.example.test.AsyncBitmapLoader;
import com.example.test.ImageCallBack;
import com.shouxun.data.HomeBannerInfo;
import com.shouxun.microclassroom.R;


/**
 * 图片轮播adapter
 *
 * @author 
 * @data  
 * @contance  
 */
public class AdvertisementAdapter extends PagerAdapter {

    private Context context;
    private List<View> views;
    ArrayList<HomeBannerInfo> advertiseArray;
	private AsyncBitmapLoader asyncBitmapLoader;

    public AdvertisementAdapter(Context context, List<View> views,
    		ArrayList<HomeBannerInfo> advertiseArray) {
        this.context = context;
        this.views = views;
        this.advertiseArray = advertiseArray;
        this.asyncBitmapLoader = new AsyncBitmapLoader();
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return views.size();
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return (arg0 == arg1);
    }

    @Override
    public void destroyItem(View container, int position, Object object) {
        ((ViewPager) container).removeView(views.get(position));
    }

    @Override
    public Object instantiateItem(View container, int position) {
        ((ViewPager) container).addView(views.get(position), 0);
        final int index = position;
        View view = views.get(position);
        try {
        	HomeBannerInfo info = advertiseArray.get(position);
            ImageView bannerImg = (ImageView) view.findViewById(R.id.banner_img);
            String url = info.getImgurl();
            //加载网络图片
            BitmapDrawable bitmapDrawable = asyncBitmapLoader.loadBitmap(
            		url, bannerImg, new ImageCallBack() {

    					@Override
    					public void imageLoad(ImageView imageView,
    							BitmapDrawable bitmap) {
    						
    						imageView.setImageBitmap(bitmap.getBitmap());
    					}
    				});
    		if (bitmapDrawable != null) {
    			 Matrix matrix = new Matrix(); 
    			 //matrix.postScale( 630,350); //长和宽放大缩小的比例	
    			 matrix.postScale(1.7f,1.7f); //长和宽放大缩小的比例
    			  Bitmap resizeBmp = Bitmap.createBitmap(
    					  bitmapDrawable.getBitmap(),
    					  0,
    					  0,
    					  bitmapDrawable.getBitmap().getWidth(),
    					  bitmapDrawable.getBitmap().getHeight(),
    					  matrix,
    					  true);
    			  bannerImg.setImageBitmap(resizeBmp);
    		}
            // item的点击监听
            bannerImg.setOnClickListener(new OnClickListener() {
                @SuppressLint("ShowToast")
                @Override
                public void onClick(View v) {
                	HomeBannerInfo bannerImg = advertiseArray.get(index);
                    if (bannerImg != null) {
                    	Toast.makeText(context, "第"+index+"个图片被点击",Toast.LENGTH_LONG).show();
                    }

                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        return view;
    }

}
